﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GSManager
{
    public partial class Main : Form
    {
        private int mucluc;
        public DangNhap dangnhap;
        private LoaiSanPham loaisp;
        private SanPham sanpham;
        private ChiPhi chiphi;
        private PhieuBanHang phieubanhang;
        private PhieuMuaHang phieumuahang;
        private PhieuDichVu phieudichvu;
        private PhieuGiaCong phieugiacong;
     //   private List<LichSu> listlichsu;
        private LichSu lichsu;
        private DichVu dichvu;
        private ThoGiaCong thogiacong;
        private KhachHang khachhang;
        private DataSet ds;
        private int mucluc_quanly;
        private AutoCompleteStringCollection autosource;
        /* mucluc=
         * 1: bán hàng
         * 2: mua hàng
         * 3: giao dịch ngoại tệ
         */
        public Main()
        {
            InitializeComponent();
            dangnhap = new DangNhap();
            dangnhap.TaiKhoan = "admin";
            dangnhap = dangnhap.LayThongTinTaiKhoan();
            
        }
        public Main(string taikhoan)
        {
            InitializeComponent();
            dangnhap = new DangNhap();
            dangnhap.TaiKhoan =taikhoan;
            dangnhap = dangnhap.LayThongTinTaiKhoan();
           
            lichsu = new LichSu();
            lichsu.NguoiDung = dangnhap.TenTaiKhoan;
            lichsu.ThaoTac = "Đăng nhập";
            lichsu.NgayGio = DateTime.Now;
            lichsu.ThemLichSu();
         
            dataGridView_LichSu.Rows.Add();
            dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count-1].Cells[0].Value = lichsu.NgayGio;
            dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count-1].Cells[1].Value = lichsu.NguoiDung;
            dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count-1].Cells[2].Value = lichsu.ThaoTac;
        }
        private void Main_Load(object sender, EventArgs e)
        {
            QuanLyHeThong_Click(sender, e);
            sanpham = new SanPham();
            autosource = sanpham.LayDSTenSanPham();
        }

      
        #region MENU
        private void QuanLyHeThong_Click(object sender, EventArgs e)
        {
            tab_GioiThieu.Visible = true;
            tab_GiaoDich.Visible = false;
        }

        private void BanHang_Click(object sender, EventArgs e)
        {
            tab_GioiThieu.Visible = true;
            tab_GiaoDich.Visible = false;
        }

        private void MuaHang_Click(object sender, EventArgs e)
        {
            tab_GioiThieu.Visible = true;
            tab_GiaoDich.Visible = false;
        }

        private void NgoaiTe_Click(object sender, EventArgs e)
        {
            tab_GioiThieu.Visible = true;
            tab_GiaoDich.Visible = false;
        }

        private void DichVu_Click(object sender, EventArgs e)
        {
            tab_GioiThieu.Visible = true;
            tab_GiaoDich.Visible = false;
        }

        private void ChiPhi_Click(object sender, EventArgs e)
        {
            tab_GioiThieu.Visible = true;
            tab_GiaoDich.Visible = false;
        }

        private void QuanLySanPham_Click(object sender, EventArgs e)
        {
            tab_GioiThieu.Visible = true;
            tab_GiaoDich.Visible = false;
           // button_GiaoDich_HuyHD_Click(sender, e);
        }

        private void BaoCao_Click(object sender, EventArgs e)
        {
            tab_GioiThieu.Visible = true;
            tab_GiaoDich.Visible = false;
            
        }
        #endregion MENU

        #region QUAN LY HE THONG

        private void button_HeThong_Thoat_Click(object sender, EventArgs e)
        {
            Close();
        }
        #endregion

        #region BAN HANG
        private void button_BanHang_LapPhieu_Click(object sender, EventArgs e)
        {
            tab_GiaoDich.Text = "Hoạt động bán hàng";
            button_GiaoDich_LuuHD.Enabled = true;
            sanpham = new SanPham();
            autosource = sanpham.LayDSTenSanPham();
            tab_GiaoDich.Visible = true;
           
            tab_GioiThieu.Visible = false;
            dataGridView_GiaoDich.Columns.Clear();
            khachhang = new KhachHang();
            AutoCompleteStringCollection autosourcekh = khachhang.LayDanhSachTenKhachHang();
            textBox_GiaoDich_KhachHang.AutoCompleteCustomSource = autosourcekh;
            textBox_GiaoDich_KhachHang.AutoCompleteSource = AutoCompleteSource.CustomSource;
            textBox_GiaoDich_KhachHang.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            textBox_GiaoDich_TongTien.Text = "0";
            textBox_GiaoDich_ThanhToan.Text = "0";
            textBox_GiaoDich_DuNo.Text = "0";
            textBox_GiaoDich_KhachHang.Text = "Khách lẻ";
            phieubanhang = new PhieuBanHang();
            textBox_GiaoDich_SoHoaDon.Text = (phieubanhang.LaySoHoaDon()+1).ToString();
            mucluc = 1;
            // cài đặt khung nhập liệu
            // cột loại sản phẩm

            // cột 
        
            // cột tên sản phẩm
         /*   DataGridViewTextBoxColumn cot_tensp1 = new DataGridViewTextBoxColumn();
            cot_tensp1.HeaderText = "TenSanPham";
            cot_tensp1.DataPropertyName = "Tam";
            
            cot_tensp1.AutoSizeMode=DataGridViewAutoSizeColumnMode.Fill;
            dataGridView_GiaoDich.Columns.Add(cot_tensp1);
            */
            DataGridViewTextBoxColumn cot_tensp = new DataGridViewTextBoxColumn();
            cot_tensp.DataPropertyName = "TenSP";
            cot_tensp.HeaderText = "Tên sản phẩm";
            
            
            cot_tensp.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridView_GiaoDich.Columns.Add(cot_tensp);
            DataGridViewTextBoxColumn cot_SoLuong = new DataGridViewTextBoxColumn();
            cot_SoLuong.DataPropertyName = "SoLuong";
            cot_SoLuong.HeaderText = "Số lượng";
            cot_SoLuong.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridView_GiaoDich.Columns.Add(cot_SoLuong);
            DataGridViewTextBoxColumn cot_Dvt = new DataGridViewTextBoxColumn();
            cot_Dvt.DataPropertyName = "Dvt";
            cot_Dvt.HeaderText = "Đơn vị tính";
            cot_Dvt.ReadOnly = true;
            cot_Dvt.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridView_GiaoDich.Columns.Add(cot_Dvt);
            DataGridViewTextBoxColumn cot_DonGia = new DataGridViewTextBoxColumn();
            cot_DonGia.DataPropertyName = "DonGia";
            cot_DonGia.HeaderText = "Đơn giá";
            cot_DonGia.ReadOnly = true;
            cot_DonGia.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridView_GiaoDich.Columns.Add(cot_DonGia);
            DataGridViewTextBoxColumn cot_ThanhTien = new DataGridViewTextBoxColumn();
            cot_ThanhTien.DataPropertyName = "ThanhTien";
            cot_ThanhTien.HeaderText = "Thành tiền";
            cot_ThanhTien.ReadOnly = true;
            
            cot_ThanhTien.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridView_GiaoDich.Columns.Add(cot_ThanhTien);
            DataGridViewTextBoxColumn cot_MaSP = new DataGridViewTextBoxColumn();
            cot_MaSP.DataPropertyName = "MaSP";
            cot_MaSP.Visible = false;
            dataGridView_GiaoDich.Columns.Add(cot_MaSP);
            dataGridView_GiaoDich.Refresh();
        }
        #endregion

        #region CHI TIET GIAO DICH
        private void button_GiaoDich_ThemKH_Click(object sender, EventArgs e)
        {
            AddNewCustomer cuaso = new AddNewCustomer(textBox_GiaoDich_KhachHang);
            cuaso.ShowDialog();
            textBox_GiaoDich_KhachHang.Focus();
        }
        decimal tongtien=0;
        
        private void dataGridView_GiaoDich_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {

            if ((e.ColumnIndex == 1) && (dataGridView_GiaoDich.Rows[e.RowIndex].Cells[0].Value != null))
            {
                
                try
                {
                    nRow = dataGridView_GiaoDich.CurrentCell.RowIndex;
                    nCol = dataGridView_GiaoDich.CurrentCell.ColumnIndex;
                    string soluong = (string)dataGridView_GiaoDich.CurrentRow.Cells[1].EditedFormattedValue.ToString();
                 //   sanpham = new SanPham();
                    if (mucluc == 1)
                    {
                        sanpham.LaySanPhamTheoTen(dataGridView_GiaoDich.Rows[nRow].Cells[0].Value.ToString());
                        if (sanpham.SoLuong < int.Parse(soluong))
                        {
                            MessageBox.Show("Trong kho chỉ còn " + sanpham.SoLuong.ToString() + " " + sanpham.DonViTinh);
                            dataGridView_GiaoDich.Rows[nRow].Cells[nCol].Selected = true;
                            e.Cancel = true;
                            return;
                        }
                    }
                    string gia = dataGridView_GiaoDich.Rows[nRow].Cells[3].Value.ToString();
                    decimal thanhtien=int.Parse(soluong)*decimal.Parse(gia);
                   
                    dataGridView_GiaoDich.Rows[nRow].Cells[4].Value = String.Format("{0:0,0}",thanhtien);
                    tongtien = 0;
                    for (int i = 0; i < dataGridView_GiaoDich.Rows.Count-1; i++)
                    {
                        if (dataGridView_GiaoDich.Rows[i].Cells[4].Value == null) break;
                        decimal tien = decimal.Parse(dataGridView_GiaoDich.Rows[i].Cells[4].Value.ToString());
                        tongtien += tien;
                    }
                    textBox_GiaoDich_TongTien.Text = String.Format("{0:0,0}",tongtien);
                    decimal thanhtoan = decimal.Parse(textBox_GiaoDich_ThanhToan.Text);
                    decimal duno = tongtien - thanhtoan;
                    textBox_GiaoDich_DuNo.Text = String.Format("{0:0,0}", duno);
                    if (mucluc == 2)
                    {
                        textBox_GiaoDich_ThanhToan.Text = String.Format("{0:0,0}", tongtien);
                        textBox_GiaoDich_DuNo.Text = "0";
                    }

                }
                catch (Exception)
                {
                    MessageBox.Show("Bạn cần nhập đúng định dạng");
                    //  isreturn = true;
                    dataGridView_GiaoDich.Rows[nRow].Cells[nCol].Selected = true;
                    e.Cancel = true;
                }
            }
            else if (e.ColumnIndex == 0)
            {

                if (e.FormattedValue.ToString() == "") return;
                if (mucluc == 1||mucluc==2)
                {
                    sanpham = sanpham.LaySanPhamTheoTen(e.FormattedValue.ToString());
                    if (sanpham.MaSP == null)
                    {
                        MessageBox.Show("Sản phẩm này không tồn tại");
                        dataGridView_GiaoDich.Rows[nRow].Cells[nCol].Selected = true;
                        e.Cancel = true;
                        return;
                    }
                    else if (sanpham.SoLuong == 0)
                    {
                        MessageBox.Show("Sản phẩm này đã hết hàng!");
                        dataGridView_GiaoDich.Rows[nRow].Cells[nCol].Selected = true;
                        e.Cancel = true;
                        return;

                    }
                    for (int i = 0; i < nRow; i++)
                    {
                        if (dataGridView_GiaoDich.Rows[i].Cells[0].Value.ToString() == e.FormattedValue.ToString())
                        {
                           
                            MessageBox.Show("Đã tồn tại sản phẩm này trong hóa đơn!");
                            dataGridView_GiaoDich.Rows[nRow].Cells[0].Selected = true;
                            dataGridView_GiaoDich.BeginEdit(true);
                            e.Cancel = true;
                            return;
                              
                        }
                    }
                    dataGridView_GiaoDich.Rows[nRow].Cells[2].Value = sanpham.DonViTinh;
                    if (mucluc == 1) dataGridView_GiaoDich.Rows[nRow].Cells[3].Value = String.Format("{0:0,0}", sanpham.GiaBanLe);
                    else dataGridView_GiaoDich.Rows[nRow].Cells[3].Value = String.Format("{0:0,0}", sanpham.GiaNhap);
                    dataGridView_GiaoDich.Rows[nRow].Cells[5].Value = sanpham.MaSP;
                }
                else if (mucluc == 3)
                     {
                         dichvu = dichvu.LayDichVuTheoTen(e.FormattedValue.ToString());
                         if (dichvu.MaDichVu == null)
                         {
                             MessageBox.Show("Dịch vụ này không tồn tại!");
                             dataGridView_GiaoDich.Rows[nRow].Cells[nCol].Selected = true;
                             e.Cancel = true;
                         }
                         
                         dataGridView_GiaoDich.Rows[nRow].Cells[3].Value = String.Format("{0:0,0}", dichvu.DonGia);
                         
                         dataGridView_GiaoDich.Rows[nRow].Cells[5].Value = dichvu.MaDichVu;
                     }
            }   
                
            
        }
        bool isreturn = false;
        int nRow = 0, nCol = 0;
       
        #endregion

        #region QUAN LY SAN PHAM, DICH VU
        private void button_QuanLySP_ThemSP_Click(object sender, EventArgs e)
        {
            AddNewProduct cuaso = new AddNewProduct();
            cuaso.ShowDialog();
        }
        #endregion
        private ComboBox comboloaisp=null;
      
       // private int ncol=-1;
      
        private void dataGridView_GiaoDich_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            
                if (nCol == 0)
                {
                    TextBox textbox = e.Control as TextBox;
                    textbox.AutoCompleteCustomSource = autosource;
                    textbox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                    textbox.AutoCompleteSource = AutoCompleteSource.CustomSource;
                }
            
        }
        

        private void dataGridView_GiaoDich_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            nRow = e.RowIndex;
            nCol = e.ColumnIndex;
        }

        private void textBox_GiaoDich_ThanhToan_Leave(object sender, EventArgs e)
        {
            if (textBox_GiaoDich_ThanhToan.Text == "")
            {
                textBox_GiaoDich_ThanhToan.Text = "0";
            }
            else
            {
                try
                {
                    decimal tien = decimal.Parse(textBox_GiaoDich_ThanhToan.Text);
                    textBox_GiaoDich_ThanhToan.Text = String.Format("{0:0,0}", tien);
                }
                catch (Exception)
                {
                    MessageBox.Show("Bạn nhập sai định dạng!");
                    textBox_GiaoDich_ThanhToan.Focus();
                }
                
            }
            decimal duno = decimal.Parse(textBox_GiaoDich_TongTien.Text) - decimal.Parse(textBox_GiaoDich_ThanhToan.Text);
            if (textBox_GiaoDich_KhachHang.Text == "Khách lẻ" && duno > 0)
            {
                MessageBox.Show("Khách lẻ không được phép nợ!");
                textBox_GiaoDich_ThanhToan.Focus();
                return;
            }
            textBox_GiaoDich_DuNo.Text = String.Format("{0:0,0}", duno);
        }

        private void button_GiaoDich_LuuHD_Click(object sender, EventArgs e)
        {
            switch (mucluc)
            {
                case 1:// lưu phiếu bán hàng
                    {
                        phieubanhang = new PhieuBanHang();
                        phieubanhang.SoPhieu = int.Parse(textBox_GiaoDich_SoHoaDon.Text);
                        if (textBox_GiaoDich_KhachHang.Text == "Khách lẻ") phieubanhang.MaKhachHang = "KhachLe";
                         else phieubanhang.MaKhachHang = khachhang.MaKH;
                        phieubanhang.NgayBan = dateTimePicker_GiaoDich_NgayGiaoDich.Value.Date;
                        phieubanhang.TongTien = decimal.Parse(textBox_GiaoDich_TongTien.Text);
                        phieubanhang.TienNo = decimal.Parse(textBox_GiaoDich_DuNo.Text);
                        if (phieubanhang.MaKhachHang == "KhachLe" && phieubanhang.TienNo > 0)
                        {
                            MessageBox.Show("Khách lẻ không được phép nợ!");
                            textBox_GiaoDich_ThanhToan.Focus();
                            return;
                        }
                        phieubanhang.NguoiBan = dangnhap.TenTaiKhoan;
                        
                           
                        
                           
                 
                        // thêm vào chi tiết hóa đơn
                        TonKho tonkho;
                        for (int i = 0; i < dataGridView_GiaoDich.Rows.Count-1; i++)
                        {
                            if (dataGridView_GiaoDich.Rows[i].Cells[5].Value == null)
                            {
                                if (dataGridView_GiaoDich.Rows[i].Cells[1].Value == null)
                                {
                                    continue;
                                }
                                else
                                {
                                    MessageBox.Show("Bạn chưa chọn sản phẩm để bán!");
                                    dataGridView_GiaoDich.Rows[i].Cells[0].Selected = true;
                                    dataGridView_GiaoDich.BeginEdit(true);
                                    return;
                                }
                            }
                            string masp=dataGridView_GiaoDich.Rows[i].Cells[5].Value.ToString();
                            if (dataGridView_GiaoDich.Rows[i].Cells[1].Value == null)
                            {
                                MessageBox.Show("Bạn chưa nhập số lượng!");
                                dataGridView_GiaoDich.Rows[i].Cells[1].Selected = true;
                                dataGridView_GiaoDich.BeginEdit(true);
                                return;
                            }
                            
                            int soluong=int.Parse(dataGridView_GiaoDich.Rows[i].Cells[1].Value.ToString());
                            decimal thanhtien=decimal.Parse(dataGridView_GiaoDich.Rows[i].Cells[4].Value.ToString());
                            tonkho = new TonKho();
                            tonkho.MaSanPham = masp;
                            tonkho.SoLuongBan = soluong;
                            tonkho.SoLuongMua = 0;
                            tonkho.Luu();
                            OleDbConnection ketnoi = KetNoi.KetNoiDuLieu();
                            string sql="Insert into CHITIETPHIEUBAN values(?,?,?,?)";
                            OleDbCommand cmd=new OleDbCommand(sql,ketnoi);
                            cmd.Parameters.Add("@SoPhieuBan",OleDbType.Integer);
                            cmd.Parameters.Add("@MaSanPham",OleDbType.VarChar);
                            cmd.Parameters.Add("@SoLuong",OleDbType.Integer);
                            cmd.Parameters.Add("@ThanhTien",OleDbType.Decimal);
                            cmd.Parameters[0].Value=phieubanhang.SoPhieu;
                            cmd.Parameters[1].Value=masp;
                            cmd.Parameters[2].Value=soluong;
                            cmd.Parameters[3].Value=thanhtien;
                            cmd.ExecuteNonQuery();
                            ketnoi.Close();
                            sanpham = new SanPham();
                            sanpham = sanpham.LaySanPhamTheoTen(dataGridView_GiaoDich.Rows[i].Cells[0].Value.ToString());
                            sanpham.SoLuong -= soluong;
                            sanpham.CapNhat();
                        }
                       
                        if (dataGridView_GiaoDich.Rows.Count == 1)
                        {
                            MessageBox.Show("Bạn chưa nhập thông tin cho phiếu bán hàng!");
                            dataGridView_GiaoDich.Rows[0].Cells[0].Selected = true;
                            dataGridView_GiaoDich.BeginEdit(true);
                            return;
                        }
                       
                        phieubanhang.ThemHoaDon();
                        if (phieubanhang.TienNo > 0)
                        {
                            ThemPhieuNo themphieuno = new ThemPhieuNo(false,textBox_GiaoDich_SoHoaDon.Text, khachhang, dateTimePicker_GiaoDich_NgayGiaoDich.Value, phieubanhang.TongTien, decimal.Parse(textBox_GiaoDich_ThanhToan.Text), dangnhap.TenTaiKhoan);
                            themphieuno.ShowDialog();
                        }
                        MessageBox.Show("Đã lưu hóa đơn!");
                        lichsu = new LichSu();
                        lichsu.NguoiDung = dangnhap.TenTaiKhoan;
                        lichsu.ThaoTac = "Bán hàng";
                        lichsu.NgayGio = DateTime.Now;
                        lichsu.ThemLichSu();

                        dataGridView_LichSu.Rows.Add();
                        dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[0].Value = lichsu.NgayGio;
                        dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[1].Value = lichsu.NguoiDung;
                        dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[2].Value = lichsu.ThaoTac;
                        button_GiaoDich_LuuHD.Enabled = false;
                    } break;
                case 2:// lưu phiếu mua hàng
                    {
                        phieumuahang = new PhieuMuaHang();
                        phieumuahang.SoPhieuMua = int.Parse(textBox_GiaoDich_SoHoaDon.Text);
                        if (textBox_GiaoDich_KhachHang.Text == "Khách lẻ") phieumuahang.MaKhachHang = "KhachLe";
                        else phieumuahang.MaKhachHang = khachhang.MaKH;
                        phieumuahang.NgayMua= dateTimePicker_GiaoDich_NgayGiaoDich.Value.Date;
                        phieumuahang.TongTien = decimal.Parse(textBox_GiaoDich_TongTien.Text);
                        //phieubanhang.TienNo = decimal.Parse(textBox_GiaoDich_DuNo.Text);
                        phieumuahang.NguoiLapPhieu = dangnhap.TenTaiKhoan;
                        TonKho tonkho;
                        for (int i = 0; i < dataGridView_GiaoDich.Rows.Count - 1; i++)
                        {
                            if (dataGridView_GiaoDich.Rows[i].Cells[5].Value == null)
                            {
                                if (dataGridView_GiaoDich.Rows[i].Cells[1].Value == null)
                                {
                                    continue;
                                }
                                else
                                {
                                    MessageBox.Show("Bạn chưa chọn sản phẩm để mua!");
                                    dataGridView_GiaoDich.Rows[i].Cells[0].Selected = true;
                                    dataGridView_GiaoDich.BeginEdit(true);
                                    return;
                                }
                            }
                            
                            string masp = dataGridView_GiaoDich.Rows[i].Cells[5].Value.ToString();
                            if (dataGridView_GiaoDich.Rows[i].Cells[1].Value == null)
                            {
                                MessageBox.Show("Bạn chưa nhập số lượng!");
                                dataGridView_GiaoDich.Rows[i].Cells[1].Selected = true;
                                dataGridView_GiaoDich.BeginEdit(true);
                                return;
                            }
                           
                            int soluong = int.Parse(dataGridView_GiaoDich.Rows[i].Cells[1].Value.ToString());
                            decimal thanhtien = decimal.Parse(dataGridView_GiaoDich.Rows[i].Cells[4].Value.ToString());
                            tonkho = new TonKho();
                            tonkho.MaSanPham = masp;
                            tonkho.SoLuongBan = 0;
                            tonkho.SoLuongMua = soluong;
                            tonkho.Luu();
                            OleDbConnection ketnoi = KetNoi.KetNoiDuLieu();
                            string sql = "Insert into CHITIETMUAHANG values(?,?,?,?)";
                            OleDbCommand cmd = new OleDbCommand(sql, ketnoi);
                            cmd.Parameters.Add("@SoPhieuMua", OleDbType.Integer);
                            cmd.Parameters.Add("@MaSanPham", OleDbType.VarChar);
                            cmd.Parameters.Add("@SoLuong", OleDbType.Integer);
                            cmd.Parameters.Add("@ThanhTien", OleDbType.Decimal);
                            cmd.Parameters[0].Value = phieumuahang.SoPhieuMua;
                            cmd.Parameters[1].Value = masp;
                            cmd.Parameters[2].Value = soluong;
                            cmd.Parameters[3].Value = thanhtien;
                            cmd.ExecuteNonQuery();
                            ketnoi.Close();
                            sanpham = new SanPham();
                            sanpham = sanpham.LaySanPhamTheoTen(dataGridView_GiaoDich.Rows[i].Cells[0].Value.ToString());
                            sanpham.SoLuong += soluong;
                            sanpham.CapNhat();
                        }
                       
                        if (dataGridView_GiaoDich.Rows.Count == 1)
                        {
                            MessageBox.Show("Bạn chưa nhập thông tin cho phiếu mua hàng!");
                            dataGridView_GiaoDich.Rows[0].Cells[0].Selected = true;
                            dataGridView_GiaoDich.BeginEdit(true);
                            return;
                        }

                        phieumuahang.ThemPhieuMuaHang();
                        MessageBox.Show("Đã lưu hóa đơn!");
                        lichsu = new LichSu();
                        lichsu.NguoiDung = dangnhap.TenTaiKhoan;
                        lichsu.ThaoTac = "Mua hàng";
                        lichsu.NgayGio = DateTime.Now;
                        lichsu.ThemLichSu();

                        dataGridView_LichSu.Rows.Add();
                        dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[0].Value = lichsu.NgayGio;
                        dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[1].Value = lichsu.NguoiDung;
                        dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[2].Value = lichsu.ThaoTac;
                        button_GiaoDich_LuuHD.Enabled = false;
                    } break;
                case 3:// lưu phiếu dịch vụ
                    {
                        phieudichvu = new PhieuDichVu();
                        phieudichvu.SoPhieuDichVu = int.Parse(textBox_GiaoDich_SoHoaDon.Text);
                        phieudichvu.NgayGiaoDich = dateTimePicker_GiaoDich_NgayGiaoDich.Value.Date;
                        if (textBox_GiaoDich_KhachHang.Text == "Khách lẻ") phieudichvu.MaKhachHang = "KhachLe";
                        else phieudichvu.MaKhachHang = khachhang.MaKH;
                        phieudichvu.TongTien = decimal.Parse(textBox_GiaoDich_TongTien.Text);
                        phieudichvu.TienNo = decimal.Parse(textBox_GiaoDich_DuNo.Text);
                        phieudichvu.NguoiLapPhieu = dangnhap.TenTaiKhoan;
                        if (phieudichvu.MaKhachHang == "KhachLe" && phieudichvu.TienNo > 0)
                        {
                            MessageBox.Show("Khách lẻ không được nợ!");
                            textBox_GiaoDich_KhachHang.Focus();
                            return;
                        }
                        DSHangGiaCong giacong;
                        
                        for (int i = 0; i < dataGridView_GiaoDich.Rows.Count-1; i++)
                        {
                            if (dataGridView_GiaoDich.Rows[i].Cells[0].Value == null)
                            {
                                MessageBox.Show("Bạn chưa nhập loại dịch vụ!");
                                dataGridView_GiaoDich.Rows[i].Cells[0].Selected = true;
                                dataGridView_GiaoDich.BeginEdit(true);
                                return;
                            }
                            if (dataGridView_GiaoDich.Rows[i].Cells[1].Value == null)
                            {
                                MessageBox.Show("Bạn chưa nhập số lượng!");

                                dataGridView_GiaoDich.Rows[i].Cells[1].Selected = true;
                                dataGridView_GiaoDich.BeginEdit(true);
                                return;
                            }
                            int soluong = int.Parse(dataGridView_GiaoDich.Rows[i].Cells[1].Value.ToString());
                            string mota = dataGridView_GiaoDich.Rows[i].Cells[2].Value.ToString();
                            decimal thanhtien = decimal.Parse(dataGridView_GiaoDich.Rows[i].Cells[4].Value.ToString());
                            string madv = dataGridView_GiaoDich.Rows[i].Cells[5].Value.ToString();
                            OleDbConnection ketnoi = KetNoi.KetNoiDuLieu();
                            string sql = "Insert into CHITIETDICHVU values(?,?,?,?,?)";
                            OleDbCommand cmd = new OleDbCommand(sql, ketnoi);
                            cmd.Parameters.Add("@SoPhieuDV", OleDbType.Integer);
                            cmd.Parameters.Add("@MaDichVu", OleDbType.VarChar);
                            cmd.Parameters.Add("@SoLuong", OleDbType.Integer);
                            cmd.Parameters.Add("@MoTa", OleDbType.VarWChar);
                            cmd.Parameters.Add("@ThanhTien", OleDbType.Decimal);
                            cmd.Parameters[0].Value = phieudichvu.SoPhieuDichVu;
                            cmd.Parameters[1].Value = madv;
                            cmd.Parameters[2].Value = soluong;
                            cmd.Parameters[3].Value = mota;
                            cmd.Parameters[4].Value = thanhtien;
                            cmd.ExecuteNonQuery();
                            ketnoi.Close();
                            if (dataGridView_GiaoDich.Rows[i].Cells[5].Value.ToString() == "GiaCong")
                            {
                                giacong = new DSHangGiaCong();
                                giacong.MaGiaCong = "GiaCong" + giacong.DemDS().ToString();
                                
                                giacong.SoLuong = int.Parse(dataGridView_GiaoDich.Rows[i].Cells[1].Value.ToString());
                                if (dataGridView_GiaoDich.Rows[i].Cells[2].Value == null)
                                {
                                    dataGridView_GiaoDich.Rows[i].Cells[2].Value = "";
                                }
                                giacong.MoTa = dataGridView_GiaoDich.Rows[i].Cells[2].Value.ToString();
                                giacong.DonGia = decimal.Parse(dataGridView_GiaoDich.Rows[i].Cells[3].Value.ToString());
                                giacong.ThanhTien = decimal.Parse(dataGridView_GiaoDich.Rows[i].Cells[4].Value.ToString());
                            //    giacong.MaThoGiaCong = "N";
                                giacong.ThemGiaCong();

                            }
                            
                            
                        }
                        if (dataGridView_GiaoDich.Rows.Count == 1)
                        {
                            MessageBox.Show("Bạn chưa nhập thông tin cho phiếu giao dịch!");
                            dataGridView_GiaoDich.Rows[0].Cells[0].Selected = true;
                            dataGridView_GiaoDich.BeginEdit(true);
                            return;
                        }
                        phieudichvu.LuuPhieuDichVu();
                        if (phieudichvu.TienNo > 0)
                        {
                            ThemPhieuNo themphieuno = new ThemPhieuNo(false,textBox_GiaoDich_SoHoaDon.Text, khachhang, dateTimePicker_GiaoDich_NgayGiaoDich.Value,phieudichvu.TongTien, decimal.Parse(textBox_GiaoDich_ThanhToan.Text), dangnhap.TenTaiKhoan);
                            themphieuno.ShowDialog();
                        }
                        MessageBox.Show("Đã lưu!");
                        lichsu = new LichSu();
                        lichsu.NguoiDung = dangnhap.TenTaiKhoan;
                        lichsu.ThaoTac = "Dịch vụ";
                        lichsu.NgayGio = DateTime.Now;
                        lichsu.ThemLichSu();

                        dataGridView_LichSu.Rows.Add();
                        dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[0].Value = lichsu.NgayGio;
                        dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[1].Value = lichsu.NguoiDung;
                        dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[2].Value = lichsu.ThaoTac;
                    } break;

            }
        }

        private void button_GiaoDich_HuyHD_Click(object sender, EventArgs e)
        {
            switch (mucluc)
            {
                case 1: button_BanHang_LapPhieu_Click(sender, e); break;
                case 2: button_MuaHang_LapPhieuMuaHang_Click(sender, e); break;
                case 3: button_DichVu_LapPhieuGiaoDich_Click(sender, e); break;
            }
        }

        private void button_MuaHang_LapPhieuMuaHang_Click(object sender, EventArgs e)
        {
            button_BanHang_LapPhieu_Click(sender, e);

            // PHieu mua hang lay so hóa đơn
            phieumuahang = new PhieuMuaHang();
            textBox_GiaoDich_SoHoaDon.Text = (phieumuahang.DemSoPhieuMuaHang() + 1).ToString();
            mucluc = 2;
            tab_GiaoDich.Text = "Hoạt động mua hàng";
        }

        private void button_DichVu_LapPhieuGiaoDich_Click(object sender, EventArgs e)
        {
            tab_GiaoDich.Text = "Hoạt động dịch vụ";
            tab_QuanLyHeThong.Visible = false;
            tab_ChiPhi.Visible = false;
            tab_GiaoDich.Visible = true;
            tab_GioiThieu.Visible = false;
            tab_HeThong.Visible = false;
            tab_PhieuGiaCong.Visible = false;
            tab_QuanLy.Visible = false;
            khachhang = new KhachHang();
            AutoCompleteStringCollection autosourcekh = khachhang.LayDanhSachTenKhachHang();
            textBox_GiaoDich_KhachHang.AutoCompleteCustomSource = autosourcekh;
            textBox_GiaoDich_KhachHang.AutoCompleteSource = AutoCompleteSource.CustomSource;
            textBox_GiaoDich_KhachHang.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            dataGridView_GiaoDich.Columns.Clear();
            phieudichvu = new PhieuDichVu();
            textBox_GiaoDich_SoHoaDon.Text = (phieudichvu.LaySoPhieuDichVu() + 1).ToString();
            textBox_GiaoDich_TongTien.Text = "0";
            textBox_GiaoDich_ThanhToan.Text = "0";
            textBox_GiaoDich_DuNo.Text = "0";
            textBox_GiaoDich_KhachHang.Text = "Khách lẻ";
            dichvu = new DichVu();
            autosource = dichvu.LayDanhSachTenDV();

            /*  phieubanhang = new PhieuBanHang();
            textBox_GiaoDich_SoHoaDon.Text = phieubanhang.LaySoHoaDon().ToString();*/
            mucluc = 3;
            /////////////////////////////////////////////////
            DataGridViewTextBoxColumn cot_tendv = new DataGridViewTextBoxColumn();
            cot_tendv.DataPropertyName = "TenDV";
            cot_tendv.HeaderText = "Tên dịch vụ";


            cot_tendv.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridView_GiaoDich.Columns.Add(cot_tendv);
            DataGridViewTextBoxColumn cot_SoLuongdv = new DataGridViewTextBoxColumn();
            cot_SoLuongdv.DataPropertyName = "SoLuongdv";
            cot_SoLuongdv.HeaderText = "Số lượng";
            cot_SoLuongdv.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridView_GiaoDich.Columns.Add(cot_SoLuongdv);
            DataGridViewTextBoxColumn cot_Dvtdv = new DataGridViewTextBoxColumn();
            cot_Dvtdv.HeaderText = "Mô tả";
            cot_Dvtdv.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
           
            dataGridView_GiaoDich.Columns.Add(cot_Dvtdv);
            DataGridViewTextBoxColumn cot_DonGiadv = new DataGridViewTextBoxColumn();
            cot_DonGiadv.DataPropertyName = "DonGiadv";
            cot_DonGiadv.HeaderText = "Đơn giá dịch vụ";
            cot_DonGiadv.ReadOnly = true;
            cot_DonGiadv.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridView_GiaoDich.Columns.Add(cot_DonGiadv);
            DataGridViewTextBoxColumn cot_ThanhTiendv = new DataGridViewTextBoxColumn();
            cot_ThanhTiendv.DataPropertyName = "ThanhTiendv";
            cot_ThanhTiendv.HeaderText = "Thành tiền";
            cot_ThanhTiendv.ReadOnly = true;

            cot_ThanhTiendv.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridView_GiaoDich.Columns.Add(cot_ThanhTiendv);
            DataGridViewTextBoxColumn cot_Madv = new DataGridViewTextBoxColumn();
            cot_Madv.DataPropertyName = "Madv";
            cot_Madv.Visible = false;
            dataGridView_GiaoDich.Columns.Add(cot_Madv);
            dataGridView_GiaoDich.Refresh();
           
        }

        private void button_DichVu_ThemDichVu_Click(object sender, EventArgs e)
        {
            tab_QuanLyHeThong.Visible = true;
            tab_GiaoDich.Visible = false;
            tab_GioiThieu.Visible = false;
            tab_QuanLy.Visible = false;
       //     dataGridView_DichVu.Columns.Clear();
          
         
            CapNhatDanhSachDichVu();
            ThemDichVu cuaso = new ThemDichVu(this);

            cuaso.ShowDialog();    
        }
       
        private void button_ChiPhi_LapPhieu_Click(object sender, EventArgs e)
        {
            tab_QuanLyHeThong.Visible = false;
            tab_ChiPhi.Visible = true;
            tab_GiaoDich.Visible = false;
            tab_GioiThieu.Visible = false;
            tab_HeThong.Visible = false;
            tab_PhieuGiaCong.Visible = false;
            tab_QuanLy.Visible = false;
            button_ChiPhi_Luu.Enabled = true;
            dataGridView_ChiPhi.Columns.Clear();
            chiphi = new ChiPhi();
            textBox_ChiPhi_SoPhieuChi.Text = (chiphi.DemSoPhieuChi() + 1).ToString();
            textBox_ChiPhi_TongTien.Text = "0";
            DataGridViewTextBoxColumn cot_TenKhoanChi = new DataGridViewTextBoxColumn();
            cot_TenKhoanChi.HeaderText = "Tên khoản chi";
            cot_TenKhoanChi.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridView_ChiPhi.Columns.Add(cot_TenKhoanChi);
            DataGridViewTextBoxColumn cot_ThanhTien = new DataGridViewTextBoxColumn();
            cot_ThanhTien.HeaderText = "Tiền chi";
            cot_ThanhTien.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridView_ChiPhi.Columns.Add(cot_ThanhTien);
            DataGridViewTextBoxColumn cot_GhiChu = new DataGridViewTextBoxColumn();
            cot_GhiChu.HeaderText = "Ghi chú";
            cot_GhiChu.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridView_ChiPhi.Columns.Add(cot_GhiChu);
            mucluc = 4;

        }

        private void dataGridView_ChiPhi_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (e.ColumnIndex == 1&& dataGridView_ChiPhi.Rows[e.RowIndex].Cells[0].Value!=null)
            {
              
                    try
                    {
                        decimal tienchi = decimal.Parse(dataGridView_ChiPhi.Rows[e.RowIndex].Cells[1].EditedFormattedValue.ToString());
                        dataGridView_ChiPhi.Rows[e.RowIndex].Cells[1].Value = String.Format("{0:0,0}", tienchi);
                        
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Bạn cần nhập đúng định dạng!");
                        dataGridView_ChiPhi.Rows[e.RowIndex].Cells[1].Selected = true;
                        e.Cancel = true;
                        return;
                    }
                    decimal tongtien=0;
                    
                    for (int i = 0; i < dataGridView_ChiPhi.Rows.Count; i++)
                    {
                        decimal tienchi = 0;
                        if (dataGridView_ChiPhi.Rows[i].Cells[0].Value != null) tienchi = decimal.Parse(dataGridView_ChiPhi.Rows[i].Cells[1].EditedFormattedValue.ToString());
                       
                        tongtien += tienchi;
                    }
                    textBox_ChiPhi_TongTien.Text = String.Format("{0:0,0}", tongtien);
            }
        }

        private void button_QuanLy_SanPham_Click(object sender, EventArgs e)
        {
            tab_QuanLyHeThong.Visible = false;
            tab_ChiPhi.Visible = false;
            tab_GiaoDich.Visible = false;
            tab_GioiThieu.Visible = false;
            tab_HeThong.Visible = false;
            tab_PhieuGiaCong.Visible = false;
            tab_QuanLy.Visible = true;
            button_QuanLy_No.Visible = false;
            mucluc_quanly = 1;
            dataGridView_QuanLy.Columns.Clear();
            DataGridViewTextBoxColumn cot_MaSp = new DataGridViewTextBoxColumn();
            cot_MaSp.HeaderText = "Mã sản phẩm";
            cot_MaSp.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            cot_MaSp.ReadOnly = true;
            dataGridView_QuanLy.Columns.Add(cot_MaSp);
            DataGridViewTextBoxColumn cot_TenSp = new DataGridViewTextBoxColumn();
            cot_TenSp.HeaderText = "Tên sản phẩm";
            cot_TenSp.ReadOnly = true;
            cot_TenSp.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridView_QuanLy.Columns.Add(cot_TenSp);
            DataGridViewTextBoxColumn cot_GiaNhap = new DataGridViewTextBoxColumn();
            cot_GiaNhap.HeaderText = "Giá nhập";
            cot_GiaNhap.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            cot_GiaNhap.ReadOnly = true;
            dataGridView_QuanLy.Columns.Add(cot_GiaNhap);
            DataGridViewTextBoxColumn cot_GiaBanLe = new DataGridViewTextBoxColumn();
            cot_GiaBanLe.HeaderText = "Giá bán lẻ";
            cot_GiaBanLe.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            cot_GiaBanLe.ReadOnly = true;
            dataGridView_QuanLy.Columns.Add(cot_GiaBanLe);
            DataGridViewTextBoxColumn cot_SoLuong = new DataGridViewTextBoxColumn();
            cot_SoLuong.HeaderText = "Lượng tồn kho";
            cot_SoLuong.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            cot_SoLuong.ReadOnly = true;
            dataGridView_QuanLy.Columns.Add(cot_SoLuong);
            DataGridViewTextBoxColumn cot_TonTT = new DataGridViewTextBoxColumn();
            cot_TonTT.HeaderText = "Tồn tối thiểu";
            cot_TonTT.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            cot_TonTT.ReadOnly = true;
            dataGridView_QuanLy.Columns.Add(cot_TonTT);
            CapNhatDanhSachSanPham();
        }
        public void CapNhatDanhSachSanPham()
        {
            ds = new DataSet();
            sanpham = new SanPham();
            dataGridView_QuanLy.Rows.Clear();
            ds = sanpham.LayDanhSachDeCapNhat();
            DataTable dt = ds.Tables["SANPHAM"];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dataGridView_QuanLy.Rows.Add();
                DataRow row = dt.Rows[i];
                for (int j = 0; j < dataGridView_QuanLy.Columns.Count; j++) dataGridView_QuanLy.Rows[i].Cells[j].Value = row[j];
            }
        }
        public void CapNhatDanhSachDichVu()
        {
            dataGridView_QuanLy.Rows.Clear();
            DichVu dichvu = new DichVu();
            ds = new DataSet();
            ds = dichvu.LayDanhSach();
            DataTable dt = ds.Tables["LOAIDICHVU"];
            
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dataGridView_QuanLy.Rows.Add();
                DataRow row = dt.Rows[i];
                for (int j = 0; j < dataGridView_QuanLy.Columns.Count; j++) dataGridView_QuanLy.Rows[i].Cells[j].Value = row[j];
            }
        }
        public void CapNhatDanhSachKhachHang()
        {
            dataGridView_QuanLy.Rows.Clear();
            KhachHang kh = new KhachHang();
            ds = new DataSet();
            ds = kh.LayDanhSach();
            DataTable dt = ds.Tables["KHACHHANG"];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dataGridView_QuanLy.Rows.Add();
                DataRow row = dt.Rows[i];
                if (row[0].ToString() == "KhachLe") continue;
                for (int j = 0; j < dataGridView_QuanLy.Columns.Count; j++) dataGridView_QuanLy.Rows[i].Cells[j].Value = row[j];
            }
        }
        public void CapNhatDanhSachThoGiaCong()
        {
            dataGridView_QuanLy.Rows.Clear();
            ThoGiaCong thogiacong = new ThoGiaCong();
            ds = new DataSet();
            ds = thogiacong.LayDanhSach();
            DataTable dt = ds.Tables["THOGIACONG"];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dataGridView_QuanLy.Rows.Add();
                DataRow row = dt.Rows[i];
                for (int j = 0; j < dataGridView_QuanLy.Columns.Count; j++) dataGridView_QuanLy.Rows[i].Cells[j].Value = row[j];
            }
        }
        private void button_QuanLy_DichVu_Click(object sender, EventArgs e)
        {
            tab_QuanLyHeThong.Visible = false;
            tab_ChiPhi.Visible = false;
            tab_GiaoDich.Visible = false;
            tab_GioiThieu.Visible = false;
            tab_HeThong.Visible = false;
            tab_PhieuGiaCong.Visible = false;
            tab_QuanLy.Visible = true;
            button_QuanLy_No.Visible = false;
            mucluc_quanly = 2;
            dataGridView_QuanLy.Columns.Clear();
            DataGridViewTextBoxColumn cot_MaDV = new DataGridViewTextBoxColumn();
            cot_MaDV.HeaderText = "Mã dịch vụ";
            cot_MaDV.ReadOnly = true;
            cot_MaDV.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridView_QuanLy.Columns.Add(cot_MaDV);
            DataGridViewTextBoxColumn cot_TenDV = new DataGridViewTextBoxColumn();
            cot_TenDV.HeaderText = "Tên dịch vụ";
            cot_TenDV.ReadOnly = true;
            cot_TenDV.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridView_QuanLy.Columns.Add(cot_TenDV);
            DataGridViewTextBoxColumn cot_DonGia = new DataGridViewTextBoxColumn();
            cot_DonGia.HeaderText = "Đơn giá dịch vụ";
            cot_DonGia.ReadOnly = true;
            cot_DonGia.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridView_QuanLy.Columns.Add(cot_DonGia);
            CapNhatDanhSachDichVu();
        }

        private void button_QuanLy_KhachHang_Click(object sender, EventArgs e)
        {
            tab_QuanLyHeThong.Visible = false;
            tab_ChiPhi.Visible = false;
            tab_GiaoDich.Visible = false;
            tab_GioiThieu.Visible = false;
            tab_HeThong.Visible = false;
            tab_PhieuGiaCong.Visible = false;
            tab_QuanLy.Visible = true;
            button_QuanLy_No.Visible = true;
            mucluc_quanly = 3;
            dataGridView_QuanLy.Columns.Clear();
            DataGridViewTextBoxColumn cot_Makh = new DataGridViewTextBoxColumn();
            cot_Makh.HeaderText = "Mã khách hàng";
            cot_Makh.ReadOnly = true;
            cot_Makh.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridView_QuanLy.Columns.Add(cot_Makh);
            DataGridViewTextBoxColumn cot_Tenkh = new DataGridViewTextBoxColumn();
            cot_Tenkh.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            cot_Tenkh.HeaderText = "Tên khách hàng";
            cot_Tenkh.ReadOnly = true;
            dataGridView_QuanLy.Columns.Add(cot_Tenkh);
            DataGridViewTextBoxColumn cot_DiaChi = new DataGridViewTextBoxColumn();
            cot_DiaChi.ReadOnly = true;
            cot_DiaChi.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            cot_DiaChi.HeaderText = "Địa chỉ";
            dataGridView_QuanLy.Columns.Add(cot_DiaChi);
            DataGridViewTextBoxColumn cot_Sdt = new DataGridViewTextBoxColumn();
            cot_Sdt.HeaderText = "Số điện thoại";
            cot_Sdt.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            cot_Sdt.ReadOnly = true;
            dataGridView_QuanLy.Columns.Add(cot_Sdt);
            DataGridViewTextBoxColumn cot_TienNo = new DataGridViewTextBoxColumn();
            cot_TienNo.ReadOnly = true;
            cot_TienNo.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            cot_TienNo.HeaderText = "Tiền nợ";
            dataGridView_QuanLy.Columns.Add(cot_TienNo);
            CapNhatDanhSachKhachHang();
        }

        private void button_QuanLy_ThoGiaCong_Click(object sender, EventArgs e)
        {
            tab_QuanLyHeThong.Visible = false;
            tab_ChiPhi.Visible = false;
            tab_GiaoDich.Visible = false;
            tab_GioiThieu.Visible = false;
            tab_HeThong.Visible = false;
            tab_PhieuGiaCong.Visible = false;
            tab_QuanLy.Visible = true;
            button_QuanLy_No.Visible = false;
            mucluc_quanly = 4;
            dataGridView_QuanLy.Columns.Clear();
            DataGridViewTextBoxColumn cot_Makh = new DataGridViewTextBoxColumn();
            cot_Makh.HeaderText = "Mã thợ gia công";
            cot_Makh.ReadOnly = true;
            cot_Makh.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridView_QuanLy.Columns.Add(cot_Makh);
            DataGridViewTextBoxColumn cot_Tenkh = new DataGridViewTextBoxColumn();
            cot_Tenkh.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            cot_Tenkh.HeaderText = "Tên thợ gia công";
            cot_Tenkh.ReadOnly = true;
            dataGridView_QuanLy.Columns.Add(cot_Tenkh);
            DataGridViewTextBoxColumn cot_DiaChi = new DataGridViewTextBoxColumn();
            cot_DiaChi.ReadOnly = true;
            cot_DiaChi.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            cot_DiaChi.HeaderText = "Địa chỉ";
            dataGridView_QuanLy.Columns.Add(cot_DiaChi);
            DataGridViewTextBoxColumn cot_Sdt = new DataGridViewTextBoxColumn();
            cot_Sdt.HeaderText = "Số điện thoại";
            cot_Sdt.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            cot_Sdt.ReadOnly = true;
            dataGridView_QuanLy.Columns.Add(cot_Sdt);
            CapNhatDanhSachThoGiaCong();
        }

        private void button_QuanLy_Them_Click(object sender, EventArgs e)
        {
            switch (mucluc_quanly)
            {
                case 1:
                    {
                        AddNewProduct cuaso = new AddNewProduct();
                        cuaso.ShowDialog();
                        CapNhatDanhSachSanPham();
                        lichsu = new LichSu();
                        lichsu.NguoiDung = dangnhap.TenTaiKhoan;
                        lichsu.ThaoTac = "Thêm sản phẩm";
                        lichsu.NgayGio = DateTime.Now;
                        lichsu.ThemLichSu();

                        dataGridView_LichSu.Rows.Add();
                        dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[0].Value = lichsu.NgayGio;
                        dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[1].Value = lichsu.NguoiDung;
                        dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[2].Value = lichsu.ThaoTac;
                    } break;
                case 2:
                    {
                        ThemDichVu cuaso = new ThemDichVu(this);
                        cuaso.ShowDialog();
                        lichsu = new LichSu();
                        lichsu.NguoiDung = dangnhap.TenTaiKhoan;
                        lichsu.ThaoTac = "Thêm dịch vụ";
                        lichsu.NgayGio = DateTime.Now;
                        lichsu.ThemLichSu();

                        dataGridView_LichSu.Rows.Add();
                        dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[0].Value = lichsu.NgayGio;
                        dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[1].Value = lichsu.NguoiDung;
                        dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[2].Value = lichsu.ThaoTac;
                        
                    } break;
                case 3:
                    {
                        AddNewCustomer cuaso = new AddNewCustomer();
                        cuaso.ShowDialog();
                        lichsu = new LichSu();
                        lichsu.NguoiDung = dangnhap.TenTaiKhoan;
                        lichsu.ThaoTac = "Thêm khách hàng";
                        lichsu.NgayGio = DateTime.Now;
                        lichsu.ThemLichSu();

                        dataGridView_LichSu.Rows.Add();
                        dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[0].Value = lichsu.NgayGio;
                        dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[1].Value = lichsu.NguoiDung;
                        dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[2].Value = lichsu.ThaoTac;
                        CapNhatDanhSachKhachHang();

                    } break;
                case 4:
                    {
                        ThemThoGiaCong cuaso = new ThemThoGiaCong();
                        cuaso.ShowDialog();
                        lichsu = new LichSu();
                        lichsu.NguoiDung = dangnhap.TenTaiKhoan;
                        lichsu.ThaoTac = "Thêm thợ gia công";
                        lichsu.NgayGio = DateTime.Now;
                        lichsu.ThemLichSu();

                        dataGridView_LichSu.Rows.Add();
                        dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[0].Value = lichsu.NgayGio;
                        dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[1].Value = lichsu.NguoiDung;
                        dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[2].Value = lichsu.ThaoTac;
                        CapNhatDanhSachThoGiaCong();
                    } break;
            }
        }

        private void button_QuanLy_Sua_Click(object sender, EventArgs e)
        {
            switch (mucluc_quanly)
            {
                case 1:
                    {
                        sanpham = sanpham.LaySanPhamTheoTen(dataGridView_QuanLy.CurrentRow.Cells[1].Value.ToString());
                        AddNewProduct cuaso = new AddNewProduct(sanpham);
                        cuaso.ShowDialog();
                        lichsu = new LichSu();
                        lichsu.NguoiDung = dangnhap.TenTaiKhoan;
                        lichsu.ThaoTac = "Cập nhật sản phẩm";
                        lichsu.NgayGio = DateTime.Now;
                        lichsu.ThemLichSu();

                        dataGridView_LichSu.Rows.Add();
                        dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[0].Value = lichsu.NgayGio;
                        dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[1].Value = lichsu.NguoiDung;
                        dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[2].Value = lichsu.ThaoTac;
                        CapNhatDanhSachSanPham();
                    } break;
                case 2:
                    {
                        dichvu = new DichVu();
                        dichvu = dichvu.LayDichVu(dataGridView_QuanLy.CurrentRow.Cells[0].Value.ToString());
                        ThemDichVu cuaso = new ThemDichVu(dichvu);
                        cuaso.ShowDialog();
                        lichsu = new LichSu();
                        lichsu.NguoiDung = dangnhap.TenTaiKhoan;
                        lichsu.ThaoTac = "Cập nhật dịch vụ";
                        lichsu.NgayGio = DateTime.Now;
                        lichsu.ThemLichSu();

                        dataGridView_LichSu.Rows.Add();
                        dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[0].Value = lichsu.NgayGio;
                        dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[1].Value = lichsu.NguoiDung;
                        dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[2].Value = lichsu.ThaoTac;
                        CapNhatDanhSachDichVu();
                    } break;
                case 3:
                    {
                        KhachHang kh = new KhachHang();
                        kh = kh.LayKhachHangTheoTen(dataGridView_QuanLy.CurrentRow.Cells[1].Value.ToString());
                        AddNewCustomer cuaso = new AddNewCustomer(kh);
                        cuaso.ShowDialog();
                        lichsu = new LichSu();
                        lichsu.NguoiDung = dangnhap.TenTaiKhoan;
                        lichsu.ThaoTac = "Cập nhật khách hàng";
                        lichsu.NgayGio = DateTime.Now;
                        lichsu.ThemLichSu();

                        dataGridView_LichSu.Rows.Add();
                        dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[0].Value = lichsu.NgayGio;
                        dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[1].Value = lichsu.NguoiDung;
                        dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[2].Value = lichsu.ThaoTac;
                        CapNhatDanhSachKhachHang();
                    } break;
                case 4:
                    {
                        ThoGiaCong thogiacong = new ThoGiaCong();
                        thogiacong = thogiacong.LayThoGiaCong(dataGridView_QuanLy.CurrentRow.Cells[0].Value.ToString());
                        ThemThoGiaCong cuaso = new ThemThoGiaCong(thogiacong);
                        cuaso.ShowDialog();
                        lichsu = new LichSu();
                        lichsu.NguoiDung = dangnhap.TenTaiKhoan;
                        lichsu.ThaoTac = "Cập nhật thợ gia công";
                        lichsu.NgayGio = DateTime.Now;
                        lichsu.ThemLichSu();

                        dataGridView_LichSu.Rows.Add();
                        dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[0].Value = lichsu.NgayGio;
                        dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[1].Value = lichsu.NguoiDung;
                        dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[2].Value = lichsu.ThaoTac;
                        CapNhatDanhSachThoGiaCong();
                    } break;
            }
        }

        private void button_QuanLy_In_Click(object sender, EventArgs e)
        {
            InHoaDon cuaso = new InHoaDon(5+mucluc_quanly);
            cuaso.ShowDialog();
        }

       
        private void button_QuanLy_No_Click(object sender, EventArgs e)
        {
            TraNo cuaso = new TraNo(dataGridView_QuanLy.CurrentRow.Cells[0].Value.ToString(), dataGridView_QuanLy.CurrentRow.Cells[1].Value.ToString(), dangnhap.TenTaiKhoan);
            cuaso.ShowDialog();
            CapNhatDanhSachKhachHang();
        }

        private void button_GiaoDich_LapPhieuGiaCong_Click(object sender, EventArgs e)
        {
            mucluc = 5;
            tab_QuanLyHeThong.Visible = false;
            tab_ChiPhi.Visible = false;
            tab_GiaoDich.Visible = false;
            tab_GioiThieu.Visible = false;
            tab_HeThong.Visible = false;
            tab_PhieuGiaCong.Visible = true;
            tab_QuanLy.Visible = false;
            button_LapPhieuGiaCong_Luu.Enabled = true;
            phieugiacong = new PhieuGiaCong();
            textBox_LapPhieuGiaCong_SoPhieu.Text = (phieugiacong.LaySoPhieuGiaCong() + 1).ToString();
             thogiacong = new ThoGiaCong();
            autosource = thogiacong.LayDanhSachTenTho();
            textBox_LapPhieuGiaCong_TenTho.AutoCompleteCustomSource = autosource;
            textBox_LapPhieuGiaCong_TenTho.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            textBox_LapPhieuGiaCong_TenTho.AutoCompleteSource = AutoCompleteSource.CustomSource;
            textBox_LapPhieuGiaCong_TongTien.Text = "0";
            dataGridView_LapPhieuGiaCong.Columns.Clear();
            DataGridViewCheckBoxColumn cot_chon = new DataGridViewCheckBoxColumn();
            cot_chon.HeaderText = "Chọn hàng gia công";
            cot_chon.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridView_LapPhieuGiaCong.Columns.Add(cot_chon);
            DataGridViewTextBoxColumn cot_magc = new DataGridViewTextBoxColumn();
            cot_magc.HeaderText = "Mã gia công";
            cot_magc.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            cot_magc.ReadOnly=true;
            dataGridView_LapPhieuGiaCong.Columns.Add(cot_magc);
            DataGridViewTextBoxColumn cot_MoTa = new DataGridViewTextBoxColumn();
            cot_MoTa.HeaderText = "Mô tả công việc";
            cot_MoTa.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            cot_MoTa.ReadOnly=true;
            dataGridView_LapPhieuGiaCong.Columns.Add(cot_MoTa);
            DataGridViewTextBoxColumn cot_SoLuong = new DataGridViewTextBoxColumn();
            cot_SoLuong.ReadOnly = true;
            cot_SoLuong.HeaderText = "Số lượng";
            cot_SoLuong.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridView_LapPhieuGiaCong.Columns.Add(cot_SoLuong);
            DataGridViewTextBoxColumn cot_DonGia = new DataGridViewTextBoxColumn();
            cot_DonGia.HeaderText = "Đơn giá";
            cot_DonGia.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            cot_DonGia.ReadOnly = true;
            dataGridView_LapPhieuGiaCong.Columns.Add(cot_DonGia);
            DataGridViewTextBoxColumn cot_ThanhTien = new DataGridViewTextBoxColumn();
            cot_ThanhTien.HeaderText = "Thành tiền";
            cot_ThanhTien.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            cot_ThanhTien.ReadOnly = true;
            dataGridView_LapPhieuGiaCong.Columns.Add(cot_ThanhTien);
            DSHangGiaCong giacong = new DSHangGiaCong();
            ds = new DataSet();
            ds = giacong.LayDanhSach();
            DataTable dt = ds.Tables["GIACONG"];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dataGridView_LapPhieuGiaCong.Rows.Add();
                DataRow row = dt.Rows[i];
                for (int j = 0; j < dataGridView_LapPhieuGiaCong.Columns.Count-1; j++) dataGridView_LapPhieuGiaCong.Rows[i].Cells[j+1].Value = row[j];
            }
           // textBox_LapPhieuGiaCong_TenTho.au
        }

      

        private void textBox_GiaoDich_KhachHang_Leave(object sender, EventArgs e)
        {
            if (textBox_GiaoDich_KhachHang.Text == "Khách lẻ") return;
            khachhang = khachhang.LayKhachHangTheoTen(textBox_GiaoDich_KhachHang.Text);
            if (khachhang.MaKH == null)
            {
                MessageBox.Show("Không tồn tại khách hàng này trong cơ sở dữ liệu!");
                textBox_GiaoDich_KhachHang.Focus();
                return;
            }
            textBox_GiaoDich_DiaChi.Text = khachhang.DiaChi;
        }

        private void textBox_LapPhieuGiaCong_TenTho_Leave(object sender, EventArgs e)
        {
            if (textBox_LapPhieuGiaCong_TenTho.Text == "") return;
           
                thogiacong = thogiacong.LayThoGiaCongTheoTen(textBox_LapPhieuGiaCong_TenTho.Text);
                if(thogiacong.MaThoGiaCong==null)
                {
                    MessageBox.Show("Tên thợ này không tồn tại");
                    textBox_LapPhieuGiaCong_TenTho.Focus();
                    return;
                }
                textBox_LapPhieuGiaCong_DiaChiTho.Text = thogiacong.DiaChi;          
        }

        private void button_LapPhieuGiaCong_Luu_Click(object sender, EventArgs e)
        {
            if (dataGridView_LapPhieuGiaCong.Rows.Count == 0)
            {
                MessageBox.Show("Hiện tại không có hàng gia công!");
                return;
            }
            if (textBox_LapPhieuGiaCong_TenTho.Text == "")
            {
                MessageBox.Show("Bạn chưa nhập tên thợ gia công!");
                textBox_LapPhieuGiaCong_TenTho.Focus();
                return;
            }
            if (dateTimePicker_NgayGiaoHang.Value <= dateTimePicker_NgayNhanHang.Value)
            {
                MessageBox.Show("Ngày giao hàng phải sau ngày nhận hàng!");
                dateTimePicker_NgayGiaoHang.Focus();
                return;
            }
            phieugiacong.SoPhieuGC = int.Parse(textBox_LapPhieuGiaCong_SoPhieu.Text);
            phieugiacong.MaTho = thogiacong.MaThoGiaCong;
            phieugiacong.NgayGiaCong = dateTimePicker_NgayNhanHang.Value;
            phieugiacong.NgayGiaoHang = dateTimePicker_NgayGiaoHang.Value;
            phieugiacong.TongTien = decimal.Parse(textBox_LapPhieuGiaCong_TongTien.Text);
            phieugiacong.NguoiLapPhieu = dangnhap.TenTaiKhoan;
           
           
            int dem = 0;
            for (int i = 0; i < dataGridView_LapPhieuGiaCong.Rows.Count; i++)
            {

                if (dataGridView_LapPhieuGiaCong.Rows[i].Cells[0].Value!=null)
                {
                    OleDbConnection connect = KetNoi.KetNoiDuLieu();
                    string sql = "Insert into CHITIETGIACONG values(?,?,?,?,?)";
                    OleDbCommand cmd = new OleDbCommand(sql, connect);
                    cmd.Parameters.Add("@SoPhieuGC", OleDbType.Integer);
                    cmd.Parameters.Add("@MaGiaCong", OleDbType.VarChar);
                    cmd.Parameters.Add("@SoLuong", OleDbType.Integer);
                    cmd.Parameters.Add("@MoTa", OleDbType.VarWChar);
                    cmd.Parameters.Add("@ThanhTien", OleDbType.Decimal);
                    cmd.Parameters[0].Value = phieugiacong.SoPhieuGC;
                    cmd.Parameters[1].Value = dataGridView_LapPhieuGiaCong.Rows[i].Cells[1].Value.ToString();
                    cmd.Parameters[2].Value = int.Parse(dataGridView_LapPhieuGiaCong.Rows[i].Cells[3].Value.ToString());
                    cmd.Parameters[3].Value = dataGridView_LapPhieuGiaCong.Rows[i].Cells[2].Value.ToString();
                    cmd.Parameters[4].Value = dataGridView_LapPhieuGiaCong.Rows[i].Cells[4].Value.ToString();
                    cmd.ExecuteNonQuery();
                    DSHangGiaCong giacong = new DSHangGiaCong();
                    giacong.CapNhat(dataGridView_LapPhieuGiaCong.Rows[i].Cells[1].Value.ToString(), thogiacong.MaThoGiaCong);
                    connect.Close();
                    dem++;
                }
            }
           
            if (dem==0)
            {
                MessageBox.Show("Bạn chưa chọn hàng gia công!");
                dataGridView_LapPhieuGiaCong.Rows[0].Cells[0].Selected = true;
                dataGridView_LapPhieuGiaCong.BeginEdit(true);
                return;
            }
            phieugiacong.LuuPhieuGiaCong();
            
            
            MessageBox.Show("Lưu phiếu thành công!");
            lichsu = new LichSu();
            lichsu.NguoiDung = dangnhap.TenTaiKhoan;
            lichsu.ThaoTac = "Lập phiếu gia công";
            lichsu.NgayGio = DateTime.Now;
            lichsu.ThemLichSu();

            dataGridView_LichSu.Rows.Add();
            dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[0].Value = lichsu.NgayGio;
            dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[1].Value = lichsu.NguoiDung;
            dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[2].Value = lichsu.ThaoTac;
            button_LapPhieuGiaCong_Luu.Enabled = false;
        }

        private void button_LapPhieuGiaCong_Huy_Click(object sender, EventArgs e)
        {
            button_GiaoDich_LapPhieuGiaCong_Click(sender, e);
        }

        private void button_ChiPhi_Luu_Click(object sender, EventArgs e)
        {
            chiphi = new ChiPhi();
            chiphi.SoPhieuChi = int.Parse(textBox_ChiPhi_SoPhieuChi.Text);
            chiphi.NgayChi = dateTimePicker_ChiPhi.Value;
            chiphi.TongTien = decimal.Parse(textBox_ChiPhi_TongTien.Text);
            chiphi.NguoiChi = dangnhap.TenTaiKhoan;
            OleDbConnection connect = KetNoi.KetNoiDuLieu();
            for (int i = 0; i < dataGridView_ChiPhi.Rows.Count-1; i++)
            {
                if (dataGridView_ChiPhi.Rows[i].Cells[0].Value == null)
                {
                    MessageBox.Show("Bạn chưa nhập tên khoản chi");
                    dataGridView_ChiPhi.Rows[i].Cells[0].Selected = true;
                    dataGridView_ChiPhi.BeginEdit(true);
                    return;
                }
                if (dataGridView_ChiPhi.Rows[i].Cells[1].Value == null)
                {
                    MessageBox.Show("Bạn chưa nhập khoản chi!");
                    dataGridView_ChiPhi.Rows[i].Cells[1].Selected = true;
                    dataGridView_ChiPhi.BeginEdit(true);
                    return;
                }
                if (dataGridView_ChiPhi.Rows[i].Cells[2].Value == null)
                {
                    
                    dataGridView_ChiPhi.Rows[i].Cells[2].Value = "";
                    
                }
                string sql = "Insert into CHITIETPHIEUCHI values(?,?,?,?)";
                OleDbCommand cmd = new OleDbCommand(sql, connect);
                cmd.Parameters.Add("@SoPhieuChi", OleDbType.Integer);
                cmd.Parameters.Add("@TenPhi", OleDbType.VarWChar);
                cmd.Parameters.Add("@TienPhi", OleDbType.Decimal);
                cmd.Parameters.Add("@GhiChu", OleDbType.VarWChar);
                //cmd.Parameters.Add("@ThanhTien", OleDbType.Decimal);
                cmd.Parameters[0].Value = chiphi.SoPhieuChi;
                cmd.Parameters[1].Value = dataGridView_ChiPhi.Rows[i].Cells[0].Value.ToString();
                cmd.Parameters[2].Value = decimal.Parse(dataGridView_ChiPhi.Rows[i].Cells[1].Value.ToString());
                cmd.Parameters[3].Value = dataGridView_ChiPhi.Rows[i].Cells[2].Value.ToString();
                //cmd.Parameters[4].Value = dataGridView_LapPhieuGiaCong.Rows[i].Cells[4].Value.ToString();
                cmd.ExecuteNonQuery();
                
            }
            connect.Close();
            if (dataGridView_ChiPhi.Rows.Count==1)
            {
                MessageBox.Show("Bạn chưa nhập thông tin cho phiếu chi!");
                dataGridView_ChiPhi.Rows.Add();
                dataGridView_ChiPhi.Rows[0].Cells[0].Selected = true;
                dataGridView_ChiPhi.BeginEdit(true);
                return;
            }
            chiphi.LuuPhieu();
            MessageBox.Show("Đã lưu thành công!");
            lichsu = new LichSu();
            lichsu.NguoiDung = dangnhap.TenTaiKhoan;
            lichsu.ThaoTac = "Lập phiếu chi";
            lichsu.NgayGio = DateTime.Now;
            lichsu.ThemLichSu();

            dataGridView_LichSu.Rows.Add();
            dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[0].Value = lichsu.NgayGio;
            dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[1].Value = lichsu.NguoiDung;
            dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[2].Value = lichsu.ThaoTac;
            button_ChiPhi_Luu.Enabled = false;
        }

        private void button_BaoCao_DoanhThu_Click(object sender, EventArgs e)
        {
            FormBaoCao cuaso = new FormBaoCao(1);
            cuaso.ShowDialog();
        }
       

        private void button_BanHang_InHoaDon_Click(object sender, EventArgs e)
        {
            InHoaDon cuaso = new InHoaDon(mucluc);
            cuaso.ShowDialog();
        }

        private void button_MuaHang_InHD_Click(object sender, EventArgs e)
        {
            InHoaDon cuaso = new InHoaDon(mucluc);
            cuaso.ShowDialog();
        }

        private void button_DichVu_InHD_Click(object sender, EventArgs e)
        {
            InHoaDon cuaso = new InHoaDon(mucluc);
            cuaso.ShowDialog();
        }

        private void button_ChiPhi_In_Click(object sender, EventArgs e)
        {
            InHoaDon cuaso = new InHoaDon(mucluc);
            cuaso.ShowDialog();
        }

        private void dataGridView_LapPhieuGiaCong_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            bool ischeck;
            if (e.RowIndex == -1) return;
            if (e.ColumnIndex == 0)
            {
                if (dataGridView_LapPhieuGiaCong.Rows[e.RowIndex].Cells[0].Value == null) dataGridView_LapPhieuGiaCong.Rows[e.RowIndex].Cells[0].Value = true;
                else
                {
                    ischeck = (bool)dataGridView_LapPhieuGiaCong.Rows[e.RowIndex].Cells[0].Value;
                    dataGridView_LapPhieuGiaCong.Rows[e.RowIndex].Cells[0].Value = !ischeck;

                }
                ischeck = (bool)dataGridView_LapPhieuGiaCong.Rows[e.RowIndex].Cells[0].Value;
                if (ischeck)
                {

                    decimal tien = decimal.Parse(dataGridView_LapPhieuGiaCong.Rows[e.RowIndex].Cells[5].Value.ToString());
                    decimal tongtien = decimal.Parse(textBox_LapPhieuGiaCong_TongTien.Text);
                    tongtien += tien;
                    textBox_LapPhieuGiaCong_TongTien.Text = String.Format("{0:0,0}", tongtien);
                }
                else
                {
                    decimal tien = decimal.Parse(dataGridView_LapPhieuGiaCong.Rows[e.RowIndex].Cells[5].Value.ToString());
                    decimal tongtien = decimal.Parse(textBox_LapPhieuGiaCong_TongTien.Text);
                    tongtien -= tien;
                    textBox_LapPhieuGiaCong_TongTien.Text = String.Format("{0:0,0}", tongtien);
                }
            }
        }

        private void button_BaoCao_TonKho_Click(object sender, EventArgs e)
        {
            InHoaDon cuaso = new InHoaDon(11);
            cuaso.ShowDialog();
        }

        private void button_HeThong_DoiMatKhau_Click(object sender, EventArgs e)
        {
            tab_QuanLyHeThong.Visible = true;
            tab_DoiMatKhau.Visible = true;
            tab_ThemNguoiDung.Visible = false;
            tab_ChiPhi.Visible = false;
            tab_GiaoDich.Visible = false;
            tab_GioiThieu.Visible = false;
            tab_HeThong.Visible = false;
            tab_PhieuGiaCong.Visible = false;
            tab_QuanLy.Visible = false;
            textBox_DoiMatKhau_MatKhauCu.Focus();
        }

        private void textBox_DoiMatKhau_MatKhauCu_Leave(object sender, EventArgs e)
        {
            if (textBox_DoiMatKhau_MatKhauCu.Text == "") return;
            else
            {
                dangnhap.MatKhau = textBox_DoiMatKhau_MatKhauCu.Text;
                if (!dangnhap.KiemTraMatKhau())
                {
                    MessageBox.Show("Bạn nhập mật khẩu cũ bị sai!");
                    textBox_DoiMatKhau_MatKhauCu.Focus();
                }
            }
        }

        private void textBox_DoiMatKhau_NhapLaiMKMoi_Leave(object sender, EventArgs e)
        {
            if (textBox_DoiMatKhau_MatKhauMoi.Text == "") return;
            
                if (textBox_DoiMatKhau_NhapLaiMKMoi.Text != textBox_DoiMatKhau_MatKhauMoi.Text)
                {
                    MessageBox.Show("Mật khẩu sai!");
                    textBox_DoiMatKhau_NhapLaiMKMoi.Focus();
                }
            
        }

        private void button_DoiMatKhau_DongY_Click(object sender, EventArgs e)
        {
            if (textBox_DoiMatKhau_MatKhauCu.Text == "")
            {
                MessageBox.Show("Bạn chưa nhập mật khẩu cũ!");
                textBox_DoiMatKhau_MatKhauCu.Focus();
            }
            if (textBox_DoiMatKhau_MatKhauMoi.Text == "")
            {
                MessageBox.Show("Bạn chưa nhập mật khẩu mới!");
                textBox_DoiMatKhau_MatKhauMoi.Focus();
            }
            if (textBox_DoiMatKhau_NhapLaiMKMoi.Text == "")
            {
                MessageBox.Show("Bạn cần nhập lại mật khẩu mới!");
                textBox_DoiMatKhau_NhapLaiMKMoi.Focus();
            }
            dangnhap.MatKhau = textBox_DoiMatKhau_MatKhauMoi.Text;
            dangnhap.ThayDoiMatKhau();
            MessageBox.Show("Thay đổi thành công!");
            lichsu = new LichSu();
            lichsu.NguoiDung = dangnhap.TenTaiKhoan;
            lichsu.ThaoTac = "Đổi mật khẩu";
            lichsu.NgayGio = DateTime.Now;
            lichsu.ThemLichSu();

            dataGridView_LichSu.Rows.Add();
            dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[0].Value = lichsu.NgayGio;
            dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[1].Value = lichsu.NguoiDung;
            dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[2].Value = lichsu.ThaoTac;
            button_DoiMatKhau_NhapLai_Click(sender, e);
        }

        private void button_DoiMatKhau_NhapLai_Click(object sender, EventArgs e)
        {
            textBox_DoiMatKhau_MatKhauCu.ResetText();
            textBox_DoiMatKhau_MatKhauMoi.ResetText();
            textBox_DoiMatKhau_NhapLaiMKMoi.ResetText();
            textBox_DoiMatKhau_MatKhauCu.Focus();
        }

        private void button_HeThong_NguoiDung_Click(object sender, EventArgs e)
        {

            tab_QuanLyHeThong.Visible = true;
            tab_DoiMatKhau.Visible = false;
            tab_ThemNguoiDung.Visible =true;
            tab_ChiPhi.Visible = false;
            tab_GiaoDich.Visible = false;
            tab_GioiThieu.Visible = false;
            tab_HeThong.Visible = false;
            tab_PhieuGiaCong.Visible = false;
            tab_QuanLy.Visible = false;
            taikhoanmoi = new DangNhap();
            textBox_ThemNguoiDung_TaiKhoan.Focus();
        }
        private DangNhap taikhoanmoi;
        private void textBox_ThemNguoiDung_TaiKhoan_Leave(object sender, EventArgs e)
        {
            if (textBox_ThemNguoiDung_TaiKhoan.Text == "") return;
            taikhoanmoi.TaiKhoan = textBox_ThemNguoiDung_TaiKhoan.Text;
            if (taikhoanmoi.KiemTraTaiKhoan())
            {
                MessageBox.Show("Tài khoản này đã tồn tại! Mời nhập tài khoản khác!");
                textBox_ThemNguoiDung_TaiKhoan.Focus();
            }
            
        }

        private void textBox_ThemNguoiDung_CapDo_Leave(object sender, EventArgs e)
        {
            if (textBox_ThemNguoiDung_CapDo.Text == "") return;
            try
            {
               taikhoanmoi.CapDo=int.Parse(textBox_ThemNguoiDung_CapDo.Text);

            }
            catch (Exception)
            {
                MessageBox.Show("Cấp độ là số tự nhiên!");
                textBox_ThemNguoiDung_CapDo.Focus();
            }
        }

        private void button_ThemNguoiDung_ChapNhan_Click(object sender, EventArgs e)
        {
            if (textBox_ThemNguoiDung_TaiKhoan.Text == "")
            {
                MessageBox.Show("Bạn chưa nhập tài khoản mới");
                textBox_ThemNguoiDung_TaiKhoan.Focus();
                return;
            }
            if (textBox_ThemNguoiDung_MatKhau.Text == "")
            {
                MessageBox.Show("Bạn chưa nhập mật khẩu!");
                textBox_ThemNguoiDung_MatKhau.Focus();
                return;
            }
            if (textBox_ThemNguoiDung_NhapLaiMK.Text == "")
            {
                MessageBox.Show("Bạn chưa nhập lại mật khẩu!");
                textBox_ThemNguoiDung_NhapLaiMK.Focus();
                return;
            }
            if (textBox_ThemNguoiDung_TenNguoiDung.Text == "")
            {
                MessageBox.Show("Bạn chưa nhập tên người dùng!");
                textBox_ThemNguoiDung_TenNguoiDung.Focus();
                return;
            }
            taikhoanmoi.MatKhau = textBox_ThemNguoiDung_MatKhau.Text;
            taikhoanmoi.TenTaiKhoan = textBox_ThemNguoiDung_TenNguoiDung.Text;
            taikhoanmoi.ThemTaiKhoan();
            MessageBox.Show("Đã thêm thành công!");
            lichsu = new LichSu();
            lichsu.NguoiDung = dangnhap.TenTaiKhoan;
            lichsu.ThaoTac = "Thêm người dùng";
            lichsu.NgayGio = DateTime.Now;
            lichsu.ThemLichSu();

            dataGridView_LichSu.Rows.Add();
            dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[0].Value = lichsu.NgayGio;
            dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[1].Value = lichsu.NguoiDung;
            dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count - 1].Cells[2].Value = lichsu.ThaoTac;
            button_ThemNguoiDung_NhapLai_Click(sender, e);
        }

        private void button_ThemNguoiDung_NhapLai_Click(object sender, EventArgs e)
        {
            textBox_ThemNguoiDung_CapDo.ResetText();
            textBox_ThemNguoiDung_MatKhau.ResetText();
            textBox_ThemNguoiDung_NhapLaiMK.ResetText();
            textBox_ThemNguoiDung_TaiKhoan.ResetText();
            textBox_ThemNguoiDung_TenNguoiDung.ResetText();
            textBox_ThemNguoiDung_TaiKhoan.Focus();
        }

        private void textBox_ThemNguoiDung_NhapLaiMK_Leave(object sender, EventArgs e)
        {
            if (textBox_ThemNguoiDung_MatKhau.Text == "") return;
           
                if (textBox_ThemNguoiDung_NhapLaiMK.Text != textBox_ThemNguoiDung_MatKhau.Text)
                {
                    MessageBox.Show("Mật khẩu nhập lại không đúng!");
                    textBox_ThemNguoiDung_NhapLaiMK.Focus();
                }
            
        }

        private void button_HeThong_DangXuat_Click(object sender, EventArgs e)
        {
            lichsu = new LichSu();
            lichsu.NguoiDung = dangnhap.TenTaiKhoan;
            lichsu.ThaoTac = "Đăng xuất";
            lichsu.NgayGio = DateTime.Now;
            lichsu.ThemLichSu();
            
            dataGridView_LichSu.Rows.Add();
            dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count-1].Cells[0].Value = lichsu.NgayGio;
            dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count-1].Cells[1].Value = lichsu.NguoiDung;
            dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count-1].Cells[2].Value = lichsu.ThaoTac;
            Login cuaso = new Login(dangnhap);
            cuaso.ShowDialog();
            if (dangnhap.TaiKhoan == "") Close();
            dangnhap = dangnhap.LayThongTinTaiKhoan();
            lichsu = new LichSu();
            lichsu.NguoiDung = dangnhap.TenTaiKhoan;
            lichsu.ThaoTac = "Đăng nhập";
            lichsu.NgayGio = DateTime.Now;
            lichsu.ThemLichSu();
           
            dataGridView_LichSu.Rows.Add();
            dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count-1].Cells[0].Value = lichsu.NgayGio;
            dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count-1].Cells[1].Value = lichsu.NguoiDung;
            dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count-1].Cells[2].Value = lichsu.ThaoTac;
        }

        private void button_HeThong_TamDung_Click(object sender, EventArgs e)
        {
            lichsu = new LichSu();
            lichsu.NguoiDung = dangnhap.TenTaiKhoan;
            lichsu.ThaoTac = "Tạm dừng";
            lichsu.NgayGio = DateTime.Now;
            lichsu.ThemLichSu();
           
            dataGridView_LichSu.Rows.Add();
            dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count-1].Cells[0].Value = lichsu.NgayGio;
            dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count-1].Cells[1].Value = lichsu.NguoiDung;
            dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count-1].Cells[2].Value = lichsu.ThaoTac;
            Login cuaso = new Login(dangnhap,true);
            cuaso.ShowDialog();
            if (dangnhap.TaiKhoan == "") Close();
            dangnhap = dangnhap.LayThongTinTaiKhoan();
            lichsu = new LichSu();
            lichsu.NguoiDung = dangnhap.TenTaiKhoan;
            lichsu.ThaoTac = "Đăng nhập lại";
            lichsu.NgayGio = DateTime.Now;
            lichsu.ThemLichSu();
          
            dataGridView_LichSu.Rows.Add();
            dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count-1].Cells[0].Value = lichsu.NgayGio;
            dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count-1].Cells[1].Value = lichsu.NguoiDung;
            dataGridView_LichSu.Rows[dataGridView_LichSu.Rows.Count-1].Cells[2].Value = lichsu.ThaoTac;
        }

        private void button_LichSu_Click(object sender, EventArgs e)
        {
            FormBaoCao cuaso = new FormBaoCao(3);
            cuaso.ShowDialog();
        }

       
       

      
       

       
      
       
     
       







































    }
}
